<%@ page contentType="text/html"%>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page import="org.jasig.cas.client.authentication.AttributePrincipal"%>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CAS Example Java Web App</title>
</head>
<body>

    <h1>CAS Example Java Web App</h1>
    <p>A sample web application that exercises the CAS protocol features via the Java CAS Client.</p>
    <hr>

    <p>
        <b>Authenticated User Id:</b> <a href="logout.jsp" title="Click here to log out"><%=request.getRemoteUser()%> </a>
    </p>

    <%
        if (request.getUserPrincipal() != null)
        {
            AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
            
            final Map attributes = principal.getAttributes();
            
            if (attributes != null)
            {
                Iterator attributeNames = attributes.keySet().iterator();
                out.println("<b>Attributes:</b>");
                
                if (attributeNames.hasNext())
                {
                    out.println("<hr><table border='3pt' width='100%'>");
                    out.println("<th colspan='2'>Attributes</th>");
                    out.println("<tr><td><b>Key</b></td><td><b>Value</b></td></tr>");
                    
                    for (; attributeNames.hasNext();)
                    {
                        out.println("<tr><td>");
                        String attributeName = (String) attributeNames.next();
                        out.println(attributeName);
                        out.println("</td><td>");
                        final Object attributeValue = attributes.get(attributeName);
                        
                        if (attributeValue instanceof List)
                        {
                            final List values = (List) attributeValue;
                            out.println("<strong>Multi-valued attribute: " + values.size() + "</strong>");
                            out.println("<ul>");
                            for (Object value : values)
                            {
                                out.println("<li>" + value + "</li>");
                            }
                            out.println("</ul>");
                        } else
                        {
                            out.println(attributeValue);
                        }
                        out.println("</td></tr>");
                    }
                    out.println("</table>");
                } else
                {
                    out.print("No attributes are supplied by the CAS server.</p>");
                }
            } else
            {
                out.println("The attribute map is empty. Review your CAS filter configurations.");
            }
        } else
        {
            out.println(
                    "The user principal is empty from the request object. Review the wrapper filter configuration.");
        }
    %>
</body>
</html>
